import { createRouter,createWebHistory , RouteRecordRaw } from 'vue-router'
import LayOut from '@/components/LayOut/LayOut.vue'
import { useloginStore } from '@/store/usePinia2'

const routes:RouteRecordRaw[] = [  
    {
        path:'/',
        name:'layout',
        component:LayOut,
        redirect:'index',
        children:[
            {
                path:'index',
                name:'index',
                component:()=>import('@/views/home/index.vue'),
                meta:{name:'首页',icon:'Grid',hidden: false},
            },
            { 
                path:'jurisdiction',  
                meta:{name:'权限管理',icon:'Cpu',hidden: false,},
                // name: ' Jurisdiction',   
                redirect:'/userlist', 
                children:[
                    {
                        path:'/jurisdiction/userlist',
                        name:'userlist',
                        component:()=>import('@/views/jurisdiction/userlist.vue'),
                        meta:{name:'用户管理',icon:'UserFilled', hidden: false,},
                    },
                    {
                        path:'/jurisdiction/role',
                        name:'role',
                        component:()=>import('@/views/jurisdiction/role.vue'),
                        meta:{name:'角色管理',icon:'Avatar', hidden: false,},
                    },
                    {
                        path:'/jurisdiction/auth',
                        name:'auth',
                        component:()=>import('@/views/jurisdiction/auth.vue'),
                        meta:{name:'授权管理',icon:'Avatar', hidden: true,},
                    },
                    {
                        path:'/jurisdiction/menu',
                        name:'menu',
                        component:()=>import('@/views/jurisdiction/menu.vue'),
                        meta:{name:'菜单管理',icon:'Memo', hidden: false,},
                    }
                ]
            },
            {
                path:'user',
                name:'user',
                component:()=>import('@/views/user/index.vue'),
                meta:{name:'用户管理',icon:'Avatar', hidden: false,},
            },
            {
                path:'shop',
                name:'shop',
                component:()=>import('@/views/shop/index.vue'),
                meta:{name:'商品管理',icon:'GoodsFilled', hidden: false,},
            }
        ], 
    }, 
    {
        path: '/login', 
        name: 'login', 
        component: () => import('@/views/login/index.vue')
    },   
    {
        path: '/',
        redirect: '/index' // 设置路由重定向
    },
    
]
const router = createRouter({
    // 1. createWebHistory 创建history路由模式   2. createWebHashHistory 创建hash路由模式
    history:createWebHistory(),
    routes
})
 
// 路由守卫
router.beforeEach((to, from, next) => {
    let token:string = useloginStore().token 
    // 登录状态
    if (token) { 
        if (to.path == '/login') {
            next('/')
            console.log('已经登录');  
        }else{
            next()
        } 
    }else{
        // 未登录状态
        // let toPath = to.path
        if (to.path.indexOf('/index')!=-1 || to.path.indexOf('/jurisdiction')!=-1  || to.path.indexOf('/user')!=-1 || to.path.indexOf('/shop')!=-1) {
            next('/login')
            console.log('未登录');  
        }else{
            next()
        } 
    } 
});

export default router